﻿using ImpactWorks.FBGraph.Core;
using ImpactWorks.FBGraph.Interfaces;
using SupportLayer.Constantes.OUTROS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace BusinessLayer
{
  public class Face
  {
    private string CallbackUrl;
    public Face(string callBackUrl)
    {
      CallbackUrl = callBackUrl;
    }

    private ImpactWorks.FBGraph.Connector.Facebook GetFace()
    {
      return new ImpactWorks.FBGraph.Connector.Facebook()
      {
        AppID = FACEBOOK.APPLICATION_ID,
        Secret = FACEBOOK.APPLICATION_SECRET,
        CallBackURL = CallbackUrl,
        Permissions = new List<ImpactWorks.FBGraph.Core.FBPermissions>()
        {
          FBPermissions.email,
          FBPermissions.user_about_me,
          FBPermissions.user_birthday,
          FBPermissions.user_location,
          FBPermissions.read_friendlists,
          FBPermissions.user_website
        }
      };
    }

    public FBUser RequestPermissions(HttpRequest Request, HttpResponse Response)
    {
      FBUser currentUser = new FBUser();
      ImpactWorks.FBGraph.Connector.Facebook Facebook = GetFace();

      if (string.IsNullOrEmpty(Request.QueryString["code"]))
        Response.Redirect(Facebook.GetAuthorizationLink());
      else
      {
        Facebook.GetAccessToken(Request.QueryString["code"]);
        currentUser = Facebook.GetLoggedInUserInfo();
      }

      return currentUser;
    }

    public void PostLinkToWall(FBUser user, string Caption, string Description, string imageUrl, string Message, string Nome, string Url)
    {
      IFeedPost post = new FeedPost();

      post.Action = new FBAction() { Name = "By Bike Shop", Link = "<a href=\"https://www.bybikeshop.com.br\" title=\"By Bike Shop BH\">By Bike Shop BH</a>" };
      post.Caption = Caption;
      post.Description = Description;
      post.ImageUrl = "<a href=\"http://www.bybikeshop.com.br/Imagens" + imageUrl + "\">http://www.bybikeshop.com.br/Imagens" + imageUrl + "</a>";
      post.Message = Message;
      post.Name = Nome;
      post.Url = "<a href=\"" + Url + "\">" + Url + "</a>";

      var Face = GetFace();
      Face.PostToWall(Face.GetLoggedInUserInfo().id.GetValueOrDefault(), post);
    }

  }
}
